package cn.msi.project.service;

import java.util.Date;

import cn.common.manager.SessionManager;
import cn.common.util.DateUtil;
import cn.common.util.GeneralUtils;
import cn.msi.customer.domain.User;
import cn.msi.project.domain.Project;
import cn.msi.project.domain.ProjectProcess;
import cn.msi.project.mapper.ProjectMapper;
import cn.msi.project.mapper.ProjectProcessMapper;

public class ProjectService {

	private ProjectMapper projectMapper;

	private ProjectProcessMapper projectProcessMapper;

	/**
	 * 搜索项目业务处理方法
	 * 
	 * @param project
	 * @return
	 */
	public String searchProject(Project project) {
		User user = (User) SessionManager.getAttr("user");
		project.setCustId(user.getCustId());
		return GeneralUtils.easyUI(projectMapper.count(project), projectMapper.selectList(project));
	}

	/**
	 * 查找项目
	 * 
	 * @param project
	 * @return
	 */
	public Project findProject(Project project) {
		if (project.getProjectId() != null) {
			project = projectMapper.selectById(project.getProjectId());
		}
		return project;
	}

	/**
	 * 保存项目
	 * 
	 * @param project
	 */
	public void saveProject(Project project) {
		// 计算项目周期
		int days = DateUtil.days(project.getStartDate(), project.getEndDate());
		project.setCycle(days);
		if (project.getProjectId() != null) {
			projectMapper.update(project);
		} else {
			User user = (User) SessionManager.getAttr("user");
			project.setCustId(user.getCustId());
			project.setProjectState("C");
			project.setFlag("Y");
			projectMapper.insert(project);
		}
	}

	/**
	 * 查询项目跟踪列表
	 * 
	 * @param projectProcess
	 * @return
	 */
	public String searchProcess(ProjectProcess projectProcess) {
		return GeneralUtils.easyUI(projectProcessMapper.count(projectProcess), projectProcessMapper.selectList(projectProcess));
	}

	/**
	 * 保存项目跟进记录
	 * 
	 * @param projectProcess
	 */
	public void saveProjectProcess(ProjectProcess projectProcess) {
		projectProcess.setCreateTime(new Date());
		projectProcessMapper.insert(projectProcess);
		// 更改项目执行状态
		Project p = new Project();
		p.setProjectId(projectProcess.getProjectId());
		p.setProjectState(projectProcess.getProcessType());
		projectMapper.update(p);
	}

	public void setProjectMapper(ProjectMapper projectMapper) {
		this.projectMapper = projectMapper;
	}

	public void setProjectProcessMapper(ProjectProcessMapper projectProcessMapper) {
		this.projectProcessMapper = projectProcessMapper;
	}
}
